package com.leve.admin.controller;

import com.leve.admin.domain.Comment;
import com.leve.admin.domain.Ghost;
import com.leve.admin.domain.Reply;
import com.leve.admin.domain.ResultBase;
import com.leve.admin.service.GhostService;
import com.leve.admin.service.MomentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("content")
@CrossOrigin(origins = {"*"}, maxAge = 3600L)
@Slf4j
public class ConnetController {

    @Autowired
    private MomentService momentService;
    @Autowired
    private GhostService ghostService;

    @ApiOperation("此刻列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping("moment")
    public ResultBase moments(HttpServletRequest httpServletRequest, @PageableDefault(size = 10) Pageable pageable) {
        String statusId = httpServletRequest.getParameter("statusId");
        String name = httpServletRequest.getParameter("name");
        PageRequest request = new PageRequest(pageable.getPageNumber() - 1, pageable.getPageSize() + 2);
        return momentService.moments(request, statusId, name);
    }


    @ApiOperation("此刻评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping("comments")
    public ResultBase comments(HttpServletRequest httpServletRequest) {
        String id = httpServletRequest.getParameter("id");
        return momentService.findCommentsByPost(id);
    }


    @ApiOperation("此刻批量点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping("batchvotes")
    public ResultBase batchVotes(HttpServletRequest httpServletRequest) {
        String id = httpServletRequest.getParameter("id");
        String voteNumber = httpServletRequest.getParameter("keyValue");
        return momentService.batchVotes(id, voteNumber);
    }

    @ApiOperation("删除此刻")
    @GetMapping("del")
    public ResultBase deletePost(HttpServletRequest httpServletRequest) {
        String id = httpServletRequest.getParameter("0");
        log.info("Delete post {}", id);
        return momentService.deletePost(id);
    }


    @ApiOperation("创建此刻评论")
    @GetMapping("addcomments")
    public ResultBase createComment(HttpServletRequest httpServletRequest) {
        String pid = httpServletRequest.getParameter("post_id");
        String text = httpServletRequest.getParameter("keyValue");
        String reply_to = httpServletRequest.getParameter("reply_to");
        String gid = httpServletRequest.getParameter("gid");
        String id = httpServletRequest.getParameter("id");
        if (pid == null) pid = id;
        log.info("Delete post {}", pid);
        Comment comment = new Comment();
        //被回复的小鬼标识
        comment.setReply_to(reply_to);
        comment.setText(text);
        Ghost ghost = new Ghost();
        ghost.setId(gid);
        comment.setAuthor(ghost);
        return momentService.createComment(pid, comment);
    }


    @ApiOperation("创建评论回复")
    @GetMapping("addreply")
    public ResultBase createReply(HttpServletRequest httpServletRequest) {
        String pid = httpServletRequest.getParameter("post_id");
        String text = httpServletRequest.getParameter("keyValue");
        String reply_to = httpServletRequest.getParameter("reply_to");
        String gid = httpServletRequest.getParameter("gid");
        String id = httpServletRequest.getParameter("id");
        String comment_id = httpServletRequest.getParameter("comment_id");
        if (comment_id == null) comment_id = id;

        log.info("Delete post {}", pid);
        Reply reply = new Reply();
        reply.setReply_to(reply_to);
        reply.setText(text);
        Ghost ghost = new Ghost();
        ghost.setId(gid);
        reply.setAuthor(ghost);
        return momentService.createReply(pid, comment_id, reply);
    }

    @ApiOperation("获取客户号")
    @GetMapping("getservers")
    public ResultBase getservers() {
        return ghostService.getservers();
    }


    @ApiOperation("此刻发布")
    @GetMapping("createpost")
    public ResultBase createPost(HttpServletRequest httpServletRequest) {
        String imgUrl = httpServletRequest.getParameter("imgUrl");
        String keyValue = httpServletRequest.getParameter("keyValue");
        String gid = httpServletRequest.getParameter("gid");
        return ghostService.createpost(imgUrl,keyValue,gid);
    }


}
